package com.colin.update.mvp.main.view;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.colin.constant.Constants;
import com.colin.download.service.DownloadService;
import com.colin.update.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
	ProgressBar progressBar;
	long downloadId;
	private DownloadService.DownloadBinder mDownloadBinder;
	private Disposable mDisposable;//可以取消观察者
	private ServiceConnection mConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mDownloadBinder = (DownloadService.DownloadBinder) service;
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mDownloadBinder = null;
		}
	};
	//开始监听进度
	private void startCheckProgress(long downloadId) {
		Observable
			.interval(100, 200, TimeUnit.MILLISECONDS, Schedulers.io())//无限轮询,准备查询进度,在io线程执行
			.filter(times -> mDownloadBinder != null)
			.map(i -> mDownloadBinder.getProgress(downloadId))//获得下载进度
			.takeUntil(progress -> progress >= 100)//返回true就停止了,当进度>=100就是下载完成了
			.distinct()//去重复
			.subscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new ProgressObserver());
	}
	//观察者
	private class ProgressObserver implements Observer<Integer> {

		@Override
		public void onSubscribe(Disposable d) {
			mDisposable = d;
		}

		@Override
		public void onNext(Integer progress) {
			progressBar.setProgress(progress);//设置进度
		}

		@Override
		public void onError(Throwable throwable) {
			throwable.printStackTrace();
			Toast.makeText(MainActivity.this, "出错", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onComplete() {
			progressBar.setProgress(100);
			Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
		}
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Intent intent = new Intent(MainActivity.this, DownloadService.class);
		bindService(intent, mConnection, BIND_AUTO_CREATE);//绑定服务
		startService(intent);
		showCheckVersionDialog();
	}

	private void showCheckVersionDialog() {
		View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_checkversion, null);
		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
			.setView(view)
			.setNegativeButton("以后再说",null)
			.setPositiveButton("立即更新",null);
		AlertDialog dialog = builder.create();
		dialog.setCancelable(false);
		dialog.setTitle("软件升级");
		dialog.show();

		dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				downloadId = mDownloadBinder.startDownload(Constants.apkUrl);
				startCheckProgress(downloadId);
				Toast.makeText(MainActivity.this, "开始下载...", Toast.LENGTH_SHORT).show();
			}
		});


		TextView currentVersion = (TextView) view.findViewById(R.id.tv_current_version);
		TextView updateVersion = (TextView) view.findViewById(R.id.tv_update_version);
		TextView updateDescription = (TextView) view.findViewById(R.id.tv_update_description);
		currentVersion.setText("v0.0.8");
		updateVersion.setText("v0.0.9" );
		updateDescription.setText("version_desc");
		progressBar = (ProgressBar) view.findViewById(R.id.progress);

	}
}
